<template>
	<div class="system-role-dialog-container">
		<el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
			<el-form ref="roleDialogFormRef" :model="state.ruleForm" size="default" label-width="90px">
				<el-row :gutter="35">
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色名称">
							<el-input v-model="state.ruleForm.roleName" placeholder="请输入角色名称" clearable disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色标识">
							<template #label>
								<el-tooltip effect="dark" content="用于 `router/route.ts` meta.roles" placement="top-start">
									<span>角色标识</span>
								</el-tooltip>
							</template>
							<el-input v-model="state.ruleForm.role" placeholder="请输入权限标识" clearable disabled></el-input>
						</el-form-item>
					</el-col>
					<!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="排序">
							<el-input-number v-model="state.ruleForm.sort" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100" />
						</el-form-item>
					</el-col> -->
					<el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
						<el-form-item label="角色状态">
							<el-switch v-model="state.ruleForm.roleStatus" inline-prompt active-text="启" inactive-text="禁" disabled></el-switch>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色描述">
							<el-input v-model="state.ruleForm.roleIntro" type="textarea" placeholder="请输入权限描述" maxlength="150" disabled></el-input>
						</el-form-item>
					</el-col>
					<el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
						<el-form-item label="角色权限">
							<el-tree :data="state.menuData" :props="state.menuProps" show-checkbox class="menu-data-tree" node-key="id"  @check="currentChecked" :default-checked-keys="state.treeArr"/>
						</el-form-item>
					</el-col>
				</el-row>
			</el-form>
			<template #footer>
				<span class="dialog-footer">
					<el-button @click="onCancel" size="default">取 消</el-button>
					<el-button type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
				</span>
			</template>
		</el-dialog>
	</div>
</template>

<script setup name="systemRoleDialog">

import { ElMessage } from 'element-plus';
import axios from 'axios';
import api from "/@/api.js";

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh']);

// 定义变量内容
const roleDialogFormRef = ref();
const state = reactive({
	ruleForm: {
		roleName: '', // 权限名称
		roleSign: '', // 权限标识
		sort: 0, // 排序
		status: true, // 权限状态
		describe: '', // 权限描述
	},
	menuData: [],
	treeArr:[],
	checkedKeys: [],
	menuProps: {
		children: 'children',
		label: 'label',
	},
	dialog: {
		isShowDialog: false,
		type: '',
		title: '',
		submitTxt: '',
	},
});
const currentChecked = (nodeObj, SelectedObj) =>{
	//console.log(SelectedObj);
	console.log(SelectedObj.checkedKeys);   // 这是选中的节点的key数组
	state.checkedKeys = SelectedObj.checkedKeys;
	//console.log(SelectedObj.checkedNodes);  // 这是选中的节点数组  
};
// 打开弹窗
const openDialog = (type, row) => {
	if (type === 'edit') {
		state.ruleForm = row;
		state.ruleForm.id = row.roleId;
		state.dialog.title = '修改权限';
		state.dialog.submitTxt = '修 改';
	} else {
		state.dialog.title = '新增权限';
		state.dialog.submitTxt = '新 增';
		// 清空表单，此项需加表单验证才能使用
		// nextTick(() => {
		// 	roleDialogFormRef.value.resetFields();
		// });
	}
	state.dialog.isShowDialog = true;
	getMenuData();
};
// 关闭弹窗
const closeDialog = () => {
	state.dialog.isShowDialog = false;
};
// 取消
const onCancel = () => {
	closeDialog();
};
// 提交
const onSubmit = () => {
	axios(
		{
			method: 'post',
			url: api.commonUrl + '/rolePermission/updaterolepermissionbyid',
			data: JSON.stringify({
				roleId: state.ruleForm.id,
				permissionIds: state.checkedKeys,
			}),
			headers: {'Content-Type': 'application/json'},
		}
	)
	.then(function (response) {
		ElMessage.success('修改成功');
		emit('refresh');
	});
	closeDialog();
	// if (state.dialog.type === 'add') { }
};
// 获取菜单结构数据
const getMenuData = () => {
	/*axios({
		method: 'get',
		url: api.commonUrl + '/permission/getPermissionByModule',
			
	}).then(function (response) {
		console.log(response.data.data);
		state.menuData = JSON.parse(response.data.data);
	});*/
	axios({
		method: 'post',
		url: api.commonUrl + '/rolePermission/getpermissionbyid',
		data:{
			id:state.ruleForm.id,
		},
		headers: {
            'Content-Type': 'application/x-www-form-urlencoded',
        },
	}).then(function (response) {
		state.treeArr = response.data.data;
	});
	
	state.menuData = [
		{ id: -1, label: '用户管理', children: [ {id: 1, label:'删除用户'},{id: 2, label:'重启用户'},{id: 3, label:'获取所有用户接口（分页）'},{id: 4, label:'通过id获取用户'},{id: 5, label:'通过username获取用户列表'},{id: 6, label:'更新用户信息'},]},{ id: -1, label: '权限管理', children: [ {id: 11, label:'添加权限信息'},{id: 12, label:'删除角色'},{id: 13, label:'分页获取权限'},{id: 14, label:'重启权限'},{id: 15, label:'通过permissionname获取权限列表'},]},{ id: -1, label: '角色管理', children: [ {id: 7, label:'添加角色'},{id: 8, label:'删除角色'},{id: 9, label:'分页获取角色'},{id: 10, label:'通过rolename获取角色列表'},]},{ id: -1, label: '用户角色管理', children: [ ]},{ id: -1, label: '角色权限管理', children: [ ]},{ id: -1, label: '病虫害信息管理', children: [ ]},{ id: -1, label: '损失数据管理', children: [ ]},{ id: -1, label: '产量数据管理', children: [ ]},{ id: -1, label: '新闻数据管理', children: [ ]},{ id: -1, label: '价格数据管理', children: [ ]},{ id: -1, label: '爬虫管理', children: [ ]} 
	];
};

// 暴露变量
defineExpose({
	openDialog,
});
</script>

<style scoped lang="scss">
.system-role-dialog-container {
	.menu-data-tree {
		width: 100%;
		border: 1px solid var(--el-border-color);
		border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
		padding: 5px;
	}
}
</style>
